package com.yc.sakuralxm.uestcscanning.activity;

import android.animation.ObjectAnimator;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.yc.sakuralxm.uestcscanning.AppContext;
import com.yc.sakuralxm.uestcscanning.BaseActivity;
import com.yc.sakuralxm.uestcscanning.Car;
import com.yc.sakuralxm.uestcscanning.R;
import com.yc.sakuralxm.uestcscanning.tools.L;
import com.yc.sakuralxm.uestcscanning.tools.ListUtil;
import com.yc.sakuralxm.uestcscanning.tools.NoDoubleClickListener;
import com.yc.sakuralxm.uestcscanning.tools.OKhttpUtils;
import com.yc.sakuralxm.uestcscanning.tools.ViewUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import okhttp3.Call;
import okhttp3.RequestBody;

/**
 * Created by sakuraLxm on 2017/11/6.
 */

public class CarInfoActivity extends BaseActivity {
    private LinearLayout idLinearBottom,idLinVistor,idLinCampu,idLinearPhone,idLinearCarType; // 从下往上 底部 访客 校区 登记电话
    private LinearLayout idLinear2;
    private String car_num;
    private ImageView idSdv;
    private TextView idCarNum,idFangke1,idCampu1,idTextEmpNum, idCarType,idFangke,idPhone1,idTextOwner, idCampu, idTextEmp, idPhone, apply_visitetime, apply_departuretime, idName, idBlackList;
    private ArrayList<String> list;
    private ImageView idNot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_info);
//        IntentUtil.startActivity(getActivity(), com.kernal.plateid.MainActivity.class);
        car_num = getIntent().getStringExtra("car_num");
//        L.e(getIntent().getStringExtra("bitmapPath"));
        setTitle("车辆信息");
        Companion.setRig(getActivity(), "首页");

        bindView();
        ViewUtil.INSTANCE.setTvText(idCarNum, car_num.trim());
        init();

    }


    private void init() {
        Map<String, Object> map = ListUtil.INSTANCE.emptyHashMap();
        map.put("carNum", car_num);
        String s = JSON.toJSONString(map);

        OKhttpUtils.Companion.post(getActivity(), "http://prod20.yc-yunpass.com/uestc/jf/platform/appapi/getCarInfo", s, progressBuilde("查询中..."), new OKhttpUtils.DataCallback() {
            @Override
            public void onFailure(Call call, IOException e) {
                findViewById(R.id.idNot).setVisibility(View.INVISIBLE);
                findViewById(R.id.idTextLook).setVisibility(View.INVISIBLE);
                findViewById(R.id.idLinearBottom).setVisibility(View.INVISIBLE);
                findViewById(R.id.idLinear1).setVisibility(View.INVISIBLE);
                idLinear2.setVisibility(View.INVISIBLE);
                ViewUtil.INSTANCE.setTvText(idBlackList, "---网络异常---");
                idBlackList.setVisibility(View.VISIBLE);

            }

            @Override
            public void onSuccess(Call call, String json, JSONObject obj) throws JSONException {
                String json2 = obj.getString("data");
                Car car = ListUtil.INSTANCE.parse(json2, Car.class);
                findViewById(R.id.idNot).setVisibility(View.VISIBLE);
                findViewById(R.id.idTextLook).setVisibility(View.VISIBLE);
                findViewById(R.id.idLinearBottom).setVisibility(View.VISIBLE);
                findViewById(R.id.idLinear1).setVisibility(View.VISIBLE);

                if (ListUtil.INSTANCE.isEmpty(car.getUserInfos())) {
                    idLinear2.setVisibility(View.INVISIBLE);
                    idBlackList.setVisibility(View.VISIBLE);
                    ViewUtil.INSTANCE.setTvText(idBlackList, "---无权通行---");
                    ViewUtil.INSTANCE.setTvText(idCarType, "外来车辆");
                    idLinearBottom.setVisibility(View.INVISIBLE);
//                    AppContext.getApp().showToastCenterLong("");
                } else {


                    Car.UserInfosBean userInfosBean = car.getUserInfos().get(0);
                    if (userInfosBean.getBlack_car() == 1) {
                        ViewUtil.INSTANCE.setTvText(idBlackList, "---禁止通行---");
                        idLinear2.setVisibility(View.INVISIBLE);
                        idBlackList.setVisibility(View.VISIBLE);
                        ViewUtil.INSTANCE.setTvText(idCarType, "黑名单");
                        return;
                    }
                    ViewUtil.INSTANCE.setTvText(idCampu, userInfosBean.getParkname());

                    if (userInfosBean.getApply_state() == 2)        // state ==2 有效其他无效
                        idNot.setImageResource(R.drawable.icon_success);
                    else {
                        idLinearBottom.setVisibility(View.INVISIBLE);
                        idNot.setImageResource(R.drawable.icon_fail);}
                    idLinear2.setVisibility(View.VISIBLE);
                    ViewUtil.INSTANCE.setTvText(apply_departuretime, userInfosBean.getApply_departuretime());
                    ViewUtil.INSTANCE.setTvText(apply_visitetime, userInfosBean.getApply_visitetime());

                    ViewUtil.INSTANCE.setTvText(idTextEmp, userInfosBean.getEmployeenumber());
                    if (userInfosBean.getApply_type() == 0) {       // 教工车辆
                        ViewUtil.INSTANCE.setTvText(idPhone, userInfosBean.getUserphone());
                        ViewUtil.INSTANCE.setTvText(idName, userInfosBean.getName());
                        idLinVistor.setVisibility(View.GONE);
                        ViewUtil.INSTANCE.setTvText(idCarType, "教工车辆");
                    } else {
                        findViewById(R.id.idTextLook).setVisibility(View.INVISIBLE);

                        idLinVistor.setVisibility(View.VISIBLE);
                        ViewUtil.INSTANCE.setTvText(idFangke,userInfosBean.getVisitor_phone());
                        ViewUtil.INSTANCE.setTvText(idName, userInfosBean.getName());
                        ViewUtil.INSTANCE.setTvText(idPhone, userInfosBean.getUserphone());
                            ViewUtil.INSTANCE.setTvText(idTextOwner,"受访人:");
                        findViewById(R.id.idLinFot).setVisibility(View.GONE);
                        idPhone1.setText("受访人电话:");
                        ViewUtil.INSTANCE.setTvText(idCarType, "访客车辆");
                        ViewUtil.INSTANCE.setTvText(idPhone,  userInfosBean.getVisitor_phone());
                        if (userInfosBean.getApply_state()!=2){

                            idLinearCarType.setVisibility(View.INVISIBLE);
                            ViewUtil.INSTANCE.setTvText(idFangke,"");
                            idLinCampu.setVisibility(View.VISIBLE);
                            ViewUtil.INSTANCE.setTvText(idTextOwner,"预约时间 :");
                            ViewUtil.INSTANCE.setTvText(idName, (String) userInfosBean.getApply_visitetime());

                            ViewUtil.INSTANCE.setTvText(idTextEmpNum,"结束时间 :");
                            ViewUtil.INSTANCE.setTvText(idTextEmp, (String) userInfosBean.getApply_departuretime());
                            idLinearPhone.setVisibility(View.GONE);
                            idFangke1.setText("最近预约：");
//                            ViewUtil.setTvText(idCarType, "访客车辆");
                            ViewUtil.INSTANCE.setTvText(idCampu, "访客车辆");
                            ViewUtil.INSTANCE.setTvText(idCampu1, "车辆类型");

                            idFangke1.setTextSize(30);
                        }
                    }

                    list = ListUtil.INSTANCE.empty();
                    if (!TextUtils.isEmpty(userInfosBean.getContract()))
                        list.add(userInfosBean.getContract());

                    if (!TextUtils.isEmpty(userInfosBean.getDriverslic()))
                        list.add(userInfosBean.getDriverslic());

                    if (!TextUtils.isEmpty(userInfosBean.getDrivinglic()))
                        list.add(userInfosBean.getDrivinglic());

                    if (!TextUtils.isEmpty(userInfosBean.getMarriage()))
                        list.add(userInfosBean.getMarriage());
                    L.INSTANCE.e(list.size() + "");


                }
            }


            @Override
            public void onOther(JSONObject obj) throws JSONException {
                if (obj.getJSONObject("data").getString("userInfos") == null) {
                    idLinear2.setVisibility(View.INVISIBLE);
                    ViewUtil.INSTANCE.setTvText(idCarType, "外来车辆");
//                    AppContext.getApp().showToastCenterLong("");
                }
            }

            @Override
            public void login(JSONObject object, RequestBody body) {

            }
        });


    }

    private ProgressDialog progressBuilde(String msg) {
        ProgressDialog dialog = new ProgressDialog(getActivity());
        dialog.setMessage(msg);
        dialog.show();
        return dialog;
    }

    //    private TranslateAnimation generateTranslateAnim(BarrageItem item, int leftMargin) {
//        TranslateAnimation anim = new TranslateAnimation(leftMargin, -item.textMeasuredWidth, 0, 0);
//        anim.setDuration(item.moveSpeed);
//        anim.setInterpolator(new AccelerateDecelerateInterpolator());
//        anim.setFillAfter(true);
//        return anim;
//    }
    private Bitmap bitmap;
    private boolean fls = false;
    private ImageView idUP, idDown;

    private void bindView() {
        idCampu1= (TextView) findViewById(R.id.idCampu1);
        idLinearCarType= (LinearLayout) findViewById(R.id.idLinearCarType);
        idLinearPhone = (LinearLayout) findViewById(R.id.idLinearPhone);
        idTextEmpNum= (TextView) findViewById(R.id.idTextEmpNum);
        idFangke1= (TextView) findViewById(R.id.idFangke1);
        idLinCampu = (LinearLayout) findViewById(R.id.idLinCampu);
        idFangke = (TextView) findViewById(R.id.idFangke);
        idLinVistor= (LinearLayout) findViewById(R.id.idLinVistor);
        idPhone1= (TextView) findViewById(R.id.idPhone1);
        idTextOwner= (TextView) findViewById(R.id.idTextOwner);
        idNot = (ImageView) findViewById(R.id.idNot);
        idBlackList = (TextView) findViewById(R.id.idBlackList);
        idUP = (ImageView) findViewById(R.id.idUP);
        idDown = (ImageView) findViewById(R.id.idDown);
        idLinear2 = (LinearLayout) findViewById(R.id.idLinear2);
        idSdv = (ImageView) findViewById(R.id.idSdv);
        idName = (TextView) findViewById(R.id.idName);
        idCarNum = (TextView) findViewById(R.id.idCarNum);
        idCampu = (TextView) findViewById(R.id.idCampu);
        idCarType = (TextView) findViewById(R.id.idCarType);
        idTextEmp = (TextView) findViewById(R.id.idTextEmp);
        idPhone = (TextView) findViewById(R.id.idPhone);
        apply_visitetime = (TextView) findViewById(R.id.apply_visitetime);
        apply_departuretime = (TextView) findViewById(R.id.apply_departuretime);
        idLinearBottom = (LinearLayout) findViewById(R.id.idLinearBottom);
        if (!TextUtils.isEmpty(getIntent().getStringExtra("bitmapPath"))) {
            bitmap = BitmapFactory.decodeFile(getIntent().getStringExtra("bitmapPath"));
            idSdv.setImageBitmap(bitmap);
        }
        final ObjectAnimator anim = ObjectAnimator.ofFloat(idLinearBottom, "translationY", 0, -300);
        anim.setDuration(1000);
        anim.setInterpolator(new AccelerateDecelerateInterpolator());

        final ObjectAnimator anim2 = ObjectAnimator.ofFloat(idLinearBottom, "translationY", -300, 0);
        anim.setDuration(1000);
        anim.setInterpolator(new AccelerateDecelerateInterpolator());
//        anim.start(true);
//
//        final TranslateAnimation anim2 = new TranslateAnimation(0, 0, 300, 0);
//        anim2.setDuration(1000);
//        anim2.setInterpolator(new AccelerateDecelerateInterpolator());
////        anim2.setRepeatMode(Animation.REVERSE);
//        anim2.setFillAfter(true);
//        final Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_pop_show);

        idUP.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {

                anim.start();
                idDown.setVisibility(View.VISIBLE);
                idUP.setVisibility(View.GONE);
            }
        });
        idDown.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                L.INSTANCE.e("为啥子下不去");
//                anim.setRepeatMode(Animation.REVERSE);
//                idLinearBottom.startAnimation(anim);
                idUP.setVisibility(View.VISIBLE);
                idDown.setVisibility(View.GONE);
                anim2.start();
            }
        });
    }


    public void goLookImg(View view) {

        if (ListUtil.INSTANCE.isEmpty(list)) {
            AppContext.Companion.getApp().showToastCenterShort("没有照片");
            return;
        }
        Intent intent = new Intent(getActivity(), ListImgActivity.class);
        intent.putStringArrayListExtra("list", list);
        startActivity(intent);

//        IntentUtil.startActivity(getActivity(), ListImgActivity.class,list);
//        //TODO
    }

    public void goIn(View view) {
        Map<String, Object> map = ListUtil.INSTANCE.emptyHashMap();
        map.put("controller_name", "Susper Saiyan");
        map.put("controller_mac_addr", "FF:00:00:00:00:CA");
        map.put("plate_number", car_num);
        String s = com.alibaba.fastjson.JSONObject.toJSONString(map);
//        map.


        OKhttpUtils.Companion.post(getActivity(), "http://prod20.yc-yunpass.com:8080/controller/getAuthByPlateNumber", s, progressBuilde("请稍候..."), new OKhttpUtils.DataCallback() {
            @Override
            public void onFailure(Call call, IOException e) {
                AppContext.Companion.getApp().showToastCenterShort("请求失败请检查网络");
            }

            @Override
            public void onSuccess(Call call, String json, JSONObject obj) throws JSONException {
                AppContext.Companion.getApp().showToastCenterLong("进场确认，请手动抬杆");

            }

            @Override
            public void onOther(JSONObject obj) throws JSONException {
                if (!TextUtils.isEmpty(obj.getString("message")))
                    AppContext.Companion.getApp().showToastCenterLong(obj.getString("message"));

            }

            @Override
            public void login(JSONObject object, RequestBody body) {

            }
        });

    }

    public void goOut(View view) {

        Map<String, Object> map = ListUtil.INSTANCE.emptyHashMap();
        map.put("controller_name", "Susper Saiyan");
        map.put("controller_mac_addr", "FF:00:00:00:00:CB");
        map.put("plate_number", car_num);
        String s = com.alibaba.fastjson.JSONObject.toJSONString(map);
//        map.


        OKhttpUtils.Companion.post(getActivity(), "http://prod20.yc-yunpass.com:8080/controller/getAuthByPlateNumber", s, progressBuilde("请稍候..."), new OKhttpUtils.DataCallback() {
            @Override
            public void onFailure(Call call, IOException e) {
                AppContext.Companion.getApp().showToastCenterShort("请求失败请检查网络");

            }

            @Override
            public void onSuccess(Call call, String json, JSONObject obj) throws JSONException {
                if (!TextUtils.isEmpty(obj.getString("message")))
                    AppContext.Companion.getApp().showToastCenterLong("出场确认，请手动抬杆");

            }

            @Override
            public void onOther(JSONObject obj) throws JSONException {
                if (!TextUtils.isEmpty(obj.getString("message")))
                    AppContext.Companion.getApp().showToastCenterLong(obj.getString("message"));

            }

            @Override
            public void login(JSONObject object, RequestBody body) {

            }
        });

    }
}
